//
//  AboutAOJIViewController.m
//  AoJi
//
//  Created by IMacLife on 15/1/28.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "AboutAOJIViewController.h"

@interface AboutAOJIViewController ()

@end

@implementation AboutAOJIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"aboutbg"]];
    
    UIView *titleview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [titleview setBackgroundColor:[UIColor colorWithRed:249.0f/255.0f green:249.0f/255.0f blue:249.0f/255.0f alpha:1.0f]];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame=CGRectMake(self.view.frame.size.width/2-50, 30, 100, 30);
    titleLabel.text=@"关于澳际";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    [titleview addSubview:titleLabel];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 20, 44, 44);
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [titleview addSubview:backBtn];
    [self.view addSubview:titleview];
    
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon"]];
    icon.frame = CGRectMake(self.view.frame.size.width/2-50, CGRectGetMaxY(titleview.frame)+31, 100, 100);
    [self.view addSubview:icon];
    
    UIImageView *wenzi = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wenzi"]];
    wenzi.frame = CGRectMake(self.view.frame.size.width/2-70, CGRectGetMaxY(icon.frame)+15, 140, 60);
    [self.view addSubview:wenzi];
    

    UIButton *update = [UIButton buttonWithType:UIButtonTypeCustom];
    update.frame = CGRectMake(19, CGRectGetMaxY(wenzi.frame)+14, self.view.frame.size.width-38, 50);
    update.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"updateBtn"]];
    [update setTitle:@"检测更新" forState:UIControlStateNormal];
    [update setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    update.titleLabel.font = [UIFont systemFontOfSize:17];
    [update addTarget:self action:@selector(updateBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:update];
    
    UIImageView *gengduo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gengduo"]];
    gengduo.frame = CGRectMake(update.frame.size.width-34, 15, 20, 20);
    [update addSubview:gengduo];
    
    
    UILabel *edition = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, CGRectGetMaxY(update.frame)+15, 150, 30)];
    edition.text = @"当前软件版本:1.0";
    [self.view addSubview:edition];
    
    //专业留学资讯网"澳际出国留学"是目前国内最大最权威的出国留学资讯网,是澳际国际教育集团通过网络和广大学生互动的平台,网站通过留学资格评估,时时在线咨询,BBS论坛和电子邮件等各种形式,为网友提供最新留学资讯和互动式在线咨询服务,近2名专业留学咨询专家和各个海外大学的招生代表,将在第一时间通过网络解答您的各种留学问题.
    
    UITextView *textview = [[UITextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(edition.frame)+(self.view.frame.size.height*0.082), self.view.frame.size.width-10, 180)];
    textview.text=@"        专业留学资讯网“澳际出国留学”是目前国内最大最权威的出国留学资讯网，是澳际国际教育集团通过网络和广大学生互动的平台，网站通过留学资格评估，时时在线咨询，BBS论坛和电子邮件等各种形式，为网友提供最新留学资讯和互动式在线咨询服务，近2名专业留学咨询专家和各个海外大学的招生代表，将在第一时间通过网络解答您的各种留学问题。";
    textview.font=[UIFont systemFontOfSize:17];
    //textview.backgroundColor=[UIColor clearColor];
    textview.delegate=self;
    //不可滚动
    textview.scrollEnabled=YES;
    [self.view addSubview:textview];

}

//返回设置
-(void)backButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

//版本检测
-(void)updateBtn
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    NSString *url =@"http://itunes.apple.com/lookup?id=123456789";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
//    NSString *results = [[NSString alloc]initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
//    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[results dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: recervedData options:NSJSONReadingMutableLeaves error:nil];
    
    NSArray *infoArray = [dic objectForKey:@"results"];
    if ([infoArray count]) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        NSString *lastVersion = [releaseInfo objectForKey:@"version"];
        
        if (![lastVersion isEqualToString:currentVersion]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"亲,有新的版本更新,是否前往?" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:@"返回", nil];
            alert.tag = 1000;
            [alert show];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"此版本位最新版" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 1001;
            [alert show];
        }
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1000) {
        if (buttonIndex==1) {
            NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];
            [[UIApplication sharedApplication]openURL:url];
        }
    }
}


#pragma mark -----------textViewDelegate
//设置textview不可编辑
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
